package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketAddress;
import com.cskaoyan.market.db.domain.MarketAddressExample;
import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.db.domain.MarketUserExample;
import com.cskaoyan.market.db.mapper.MarketAddressMapper;
import com.cskaoyan.market.db.mapper.MarketUserMapper;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.regex.Pattern;

@Service
public class WxAddressServiceImpl implements WxAddressService {
    @Autowired
    MarketAddressMapper marketAddressMapper;
    @Autowired
    SecurityManager securityManager;
    @Autowired
    MarketUserMapper marketUserMapper;

    @Override
    public List<MarketAddress> list() {
        MarketAddressExample addressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andUserIdEqualTo(userId());
        criteria.andDeletedEqualTo(true);
        List<MarketAddress> addressList = marketAddressMapper.selectByExample(addressExample);
        return addressList;
    }

    @Override
    @Transactional
    public Object save(MarketAddress marketAddress) {
        marketAddress.setUserId(userId());
        marketAddress.setDeleted(true);
        if(marketAddress.getAddTime() == null){
            marketAddress.setAddTime(LocalDateTime.now());
        }
        if(marketAddress.getDeleted().equals(true)){
            addressUpdate();
        }
        marketAddress.setUpdateTime(LocalDateTime.now());
        String tel = marketAddress.getTel();
        String name = marketAddress.getName();
        String address=marketAddress.getAddressDetail();
        String province=marketAddress.getProvince();
        if(StringUtils.isEmpty(name)){
            return ResponseUtil.fail(102,"请输入姓名");
        }
        if(StringUtils.isEmpty(address)){
            return ResponseUtil.fail(103,"请输入详细地址");
        }
        if(StringUtils.isEmpty(province)){
            return ResponseUtil.fail(105,"请输入省份信息");
        }
        if (StringUtils.isBlank(tel)) {
            return ResponseUtil.fail(104,"请输入11位电话号码");
        }
        while (Pattern.matches("^1[1-9]\\d{9}$", tel)) {
            if(marketAddress1(marketAddress) !=null){
                marketAddressMapper.updateByPrimaryKeySelective(marketAddress);
                return ResponseUtil.ok(marketAddress.getId());
            }
            marketAddressMapper.insertSelective(marketAddress);
            return ResponseUtil.ok(marketAddress.getId());
        }
            return ResponseUtil.fail(401,"参数错误");
    }

    private void addressUpdate() {
        MarketAddressExample addressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andUserIdEqualTo(userId());
        List<MarketAddress>list=marketAddressMapper.selectByExample(addressExample);
        for(MarketAddress marketAddress:list){
            if(marketAddress.getIsDefault().equals(true)){
                marketAddress.setIsDefault(false);
                marketAddressMapper.updateByPrimaryKeySelective(marketAddress);
            }
        }
    }

    private Object marketAddress1(MarketAddress marketAddress) {
        MarketAddress marketAddress1=marketAddressMapper.selectByPrimaryKey(marketAddress.getId());
        return marketAddress1;
    }

    @Override
    public MarketAddress detile(Integer id) {
        MarketAddressExample addressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andIdEqualTo(id);
        criteria.andUserIdEqualTo(userId());
        MarketAddress addressList = marketAddressMapper.selectByExample(addressExample).get(0);
        return addressList;
    }

    @Override
    @Transactional
    public Integer delete(Integer id) {
        MarketAddress marketAddress =new MarketAddress();
        marketAddress.setDeleted(false);
        marketAddress.setId(id);
        marketAddress.setUpdateTime(LocalDateTime.now());
        Integer i =marketAddressMapper.updateByPrimaryKeySelective(marketAddress);
        return i;
    }

    private Integer userId() {
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MarketUser user = (MarketUser) session.getAttribute("user");
        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria userExampleCriteria = marketUserExample.createCriteria();
        userExampleCriteria.andUsernameEqualTo(user.getUsername());
        MarketUser marketUser = marketUserMapper.selectByExample(marketUserExample).get(0);
        Integer userId = marketUser.getId();
        return userId;
    }
}
